package com.jbookshelf.beans;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.jbookshelf.dal.entities.Languages;
import com.jbookshelf.dal.entities.OrderStatuses;
import com.jbookshelf.dal.entities.Orders;
import com.jbookshelf.dal.entities.Shops;
import com.jbookshelf.dal.entities.Users;
import com.jbookshelf.dal.enums.OrderState;
import com.jbookshelf.dal.providers.OrdersDataProvider;
import com.jbookshelf.dal.providers.ShopsDataProvider;
import com.jbookshelf.dal.providers.StatusesDataProvider;
import com.jbookshelf.dal.providers.UsersDataProvider;

@Component
@Scope("request")
public class OrdersBean {
	@Autowired
	OrdersDataProvider ordersDataProvider;
	@Autowired
	ShopsDataProvider shopsDataProvider;
	@Autowired
	UsersDataProvider usersDataProvider;
	@Autowired
	StatusesDataProvider statusesDataProvider;

	private List<Orders> orders;
	private Orders selectedOrder;
	private Orders newOrder;
	private List<Shops> shopslist;

	public OrdersBean()
	{
		newOrder = new Orders();
		Shops shop = new Shops();
		newOrder.setShops(shop);
	}
	public List<Shops> getShopslist() {
		if (shopslist == null) {
			shopslist = shopsDataProvider.getShops();
		}
		return shopslist;
	}
	public void setShopslist(List<Shops> shopslist) {
		this.shopslist = shopslist;
	}
	public Orders getNewOrder() {
		return newOrder;
	}

	public void setNewOrder(Orders newOrder) {
		this.newOrder = newOrder;
	}

	public List<Orders> getOrders() {
		if (orders == null) {
			orders = ordersDataProvider.getOrders();
		}
		return orders;
	}

	public void setOrders(List<Orders> orders) {
		this.orders = orders;
	}

	public Orders getSelectedOrder() {
		return selectedOrder;
	}

	public void setSelectedOrder(Orders selectedOrder) {
		this.selectedOrder = selectedOrder;
	}
	
	public void showDialog()
	{
		final RequestContext context = RequestContext.getCurrentInstance();
		context.execute("newOrderDialog.show()");
	}
	public void hideDialog()
	{
		final RequestContext context = RequestContext.getCurrentInstance();
		context.execute("newOrderDialog.hide()");
	}
	public void saveNewOrder()
	{
		Shops shop = shopsDataProvider.getShopByName(this.getNewOrder().getShops().getName());
		String login  = SecurityContextHolder.getContext().getAuthentication().getName();
		OrderStatuses status = statusesDataProvider.getStatus(OrderState.NEW); 
		this.newOrder.setShops(shop);
		this.newOrder.setUsers(usersDataProvider.getUserByLogin(login));
		this.newOrder.setOrderStatuses(status);
		ordersDataProvider.saveOrder(newOrder);
		this.hideDialog();
		RequestContext.getCurrentInstance().update("orders_form");
	}

}
